<?php
namespace Home\Controller;
use Think\Controller;
class MyselfController extends CommonController {
    /**
     * 我的--主页面
     */
   public function index(){
       $this->assign('Userinfo',$this->getUserInfo());
       $this->display();
   }

   /**
    * 打卡
    */
    public function punchcard(){
        if(IS_POST){
            $user_info=session('index_user');
            //上次签到时间小于今天的起始时间戳就可以签
            $todaytime=strtotime(date('Y-m-d'));
            if($todaytime > $user_info['last_time']){
                $info['id']=$user_info['id'];
                $user_info['usercard']=$info['usercard']=$user_info['usercard']+1;
                $user_info['last_time']=$info['last_time']=time();
                $tag2=M('user')->save($info);
                if($tag2){
                    $info['msg']='ok';
                    session('index_user',$user_info);
                }
                else
                    $info['msg']='error';
            }else{
                $info['msg']='played';//今天已经打卡
            }
            $this->ajaxReturn($info);
        }
    }

   /**
    * 修改资料
    */
    public function modifyData(){
        if(IS_GET){
            $this->assign('Userinfo',$this->getUserInfo());
            $this->display('myself_upd');
        }else{
            $mycount=0;//计数用于判断是否有改变值，未改变就加1，如果总数为5就全部未改变
            $data=I('post.');
            $have=$_FILES['userpho'];//文件
            $user_info=session('index_user');
            $info['id']=$user_info['id'];
            if($have['name']){
                //有文件上传
                $name ='userpho';
                $path=$this->uploadpho($name);
                $user_info['userpho']=$info['userpho']=$path;
                session('mycount',$path);
            }else
                $mycount += 1;
            ($data['nickname'] != $user_info['nickname']) ? ($user_info['nickname']=$info['nickname']=$data['nickname']) : $mycount += 1;
            ($data['email'] != $user_info['email']) ? ($user_info['email']=$info['email']=$data['email']) : $mycount += 1;
            ($data['phone'] != $user_info['phone']) ? ($user_info['phone']=$info['phone']=$data['phone']) : $mycount += 1;
            //旧密码是否与原来一致
            (md5($data['oldpassword']) == $user_info['password']) ? ($user_info['password']=$info['password'] = md5($data['password'])) :$mycount += 1;
            if($mycount == 5){//没有任何修改的东西
                session('mycount',$mycount);
                if($data['oldpassword'] && md5($data['oldpassword']) != $user_info['password']){
                    $this->error('旧密码不正确');
                }else
                    $this->error('无修改的内容');
            }else{
                $tag=M('user')->save($info);
                if ($tag){
                    session('index_user',$user_info);
                    $this->success('资料修改成功');
                }
                else
                    $this->error('系统繁忙，请稍后重试');
            }

        }
    }

   /**
    * 预约列表
    */
    public function taskList(){
        $this->assign('Userinfo',$this->getUserInfo());
        $map['teacherid'] = session('index_user.id');
        $list = M('plan')->where($map)->order('create_time desc')->select();

        $this->assign('list',$list);
        $this->display('myself_tasklist');
    }

    /**
     * 添加任务
     */
    public function add(){
        if(IS_POST){
            $data = I('post.');
            if($data['id']){
                $tag = M('plan')->save($data);
                if ($tag){
                    $this->success('任务编辑成功','taskList');
                }else
                    $this->error('任务编辑失败');
            }else{
                unset($data['id']);
                $data['create_time'] = time();
                $data['teacherid'] = session('index_user.id');
                $data['remark'] =trim($data['remark']);
                $data['finish_time'] =strtotime($data['finish_time']);
                $tag = M('plan')->add($data);
                if ($tag){
                    $this->success('任务添加成功','index');
                }else
                    $this->error('任务添加失败');
            }
        }else{
            if(I('id')){
                $info  = M('plan')->find(I('id'));
                $info['finish_time'] = date('Y-m-d H:i:s',$info['finish_time']);
                $this->assign('info',$info);
            }
            $this->assign('Userinfo',$this->getUserInfo());
            $this->display();
        }
    }


    /**
     * 删除任务
     */
    public function delete()
    {
        $id = I('id');
        if($id){
            if(M('plan')->delete($id)){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('错误操作');
        }
    }

}